package com.coderfans.databole;

import com.alibaba.fastjson.JSON;
import com.coderfans.databole.model.BoleBlogModel;
import com.coderfans.databole.model.BoleUserModel;
import com.coderman.daoclient.utils.DateBuilder;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import us.codecraft.xsoup.Xsoup;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * Created by 58-daojia on 2017-12-18.
 *
 * @author: fanchunshuai
 * @version: V1.0
 * @Desc:
 * @Copyright (c) 2017 58到家-版权所有
 */
public class HtmlParser {
    public static void main(String[] args) {
        Document document = null;
        try {
            File file = new File("E:\\spiderFile\\jobbole\\blog");

            BoleUserModel userModel = new BoleUserModel();



            File[] fileList = file.listFiles();
            for (File file1 : fileList){
                BoleBlogModel boleBlogModel = new BoleBlogModel();

                String fileName = file1.getName().replace(".html","");
                document = Jsoup.parse(file1, "UTF-8");

                //获取博客发布作者
                //String author = "*//*//**//*[@id=\"post-"+fileName+"\"]/div[3]/div[1]";

                String authorTag = userModel.getXpathMap().get("nickName").replace("$",fileName);


                //获取博客标题
                String titleTag = boleBlogModel.getXpathMap().get("blogTitle").replace("$",fileName);
                String titleName = Xsoup.compile(titleTag).evaluate(document).get();
                boleBlogModel.setBlogTitle(titleName);

                //发布日期
                String publishDateTag = "//*[@id=\"post-"+fileName+"\"]/div[2]/p/text()";
                String publishDate = Xsoup.compile(publishDateTag).evaluate(document).get();
                if(publishDate != null){
                    publishDate = publishDate.substring(0,10).replace("/","-");
                    boleBlogModel.setPublishDate( DateBuilder.convertStringToDate(publishDate,"yyyy-MM-dd"));
                }

                //博客分类
                String blogCategoryTag = "//*[@id=\"post-"+fileName+"\"]/div[2]/p/a[1]/text()";
                String blogCategory = Xsoup.compile(blogCategoryTag).evaluate(document).get();
                boleBlogModel.setBlogCategory(blogCategory);
                StringBuilder builder = new StringBuilder();

                //博客标签
                String publishBlogTag = "//*[@id=\"post-"+fileName+"\"]/div[2]/p/a[3]/text()";
                String publishBlog1 = Xsoup.compile(publishBlogTag).evaluate(document).get();
                if(publishBlog1 != null && !publishBlog1.contains("条评论")){
                    builder.append(publishBlog1);
                    builder.append(",");
                }

                //博客标签
                String publishBlogTag2 = "//*[@id=\"post-"+fileName+"\"]/div[2]/p/a[4]/text()";
                String publishBlog2 = Xsoup.compile(publishBlogTag2).evaluate(document).get();
                if(publishBlog2 != null){
                    builder.append(publishBlog2);
                    builder.append(",");
                }

                //博客标签
                String publishBlogTag3 = "//*[@id=\"post-"+fileName+"\"]/div[2]/p/a[5]/text()";
                String publishBlog3 = Xsoup.compile(publishBlogTag3).evaluate(document).get();

                if(publishBlog3 != null){
                    builder.append(publishBlog3);
                    builder.append(",");
                }
                if(builder.length() >0){

                    boleBlogModel.setBlogTags(builder.toString().substring(0,builder.length() - 1));
                }

                //博客评论数量
                String commentTag = "//*[@id=\"post-"+fileName+"\"]/div[2]/p/a[2]/text()";
                String commentNum = Xsoup.compile(commentTag).evaluate(document).get();
                if(commentNum != null){
                    commentNum = commentNum.replace(" 条评论","");
                }
                if(StringUtils.isNumeric(commentNum)){
                    boleBlogModel.setCommentNum(Integer.parseInt(commentNum));
                }else{

                    boleBlogModel.setBlogCategory(boleBlogModel.getBlogCategory()+","+commentNum);
                    //博客评论数量
                    commentTag = "//*[@id=\"post-"+fileName+"\"]/div[2]/p/a[3]/text()";
                    String commentStr = Xsoup.compile(commentTag).evaluate(document).get();
                    if(commentStr!= null){
                        commentNum = commentStr.replace(" 条评论","");
                        if(StringUtils.isNumeric(commentNum)){
                            boleBlogModel.setCommentNum(Integer.parseInt(commentNum));
                        }
                    }

                }


                String simpleLinksTag = "//*[@id=\"wrapper\"]/div[1]/ul//a/@href";
                List<String> simpleLinkList = Xsoup.compile(simpleLinksTag).evaluate(document).list();
                if(simpleLinkList.size() == 0){
                    String referLinksTag = "//*[@id=\"wrapper\"]/div[3]/div[2]/ul//a/@href";
                    List<String> referLinks = Xsoup.compile(referLinksTag).evaluate(document).list();
                    boleBlogModel.setSimpleLinks(JSON.toJSONString(referLinks));

                }else {
                    boleBlogModel.setSimpleLinks(JSON.toJSONString(simpleLinkList));
                }



                String  authorNameTag = Xsoup.compile(authorTag).evaluate(document).get();







                if(authorNameTag.contains("原文出处")) {

                    //博客源地址
                    String sourceLinkTag = boleBlogModel.getXpathMap().get("sourceLink").replace("$",fileName);
                    String sourceLink = Xsoup.compile(sourceLinkTag).evaluate(document).get();
                    boleBlogModel.setSourceLink(sourceLink);

                    //博客作者名称
                    String blogUserNameTag = boleBlogModel.getXpathMap().get("authorName").replace("$",fileName);
                    String blogUserName = Xsoup.compile(blogUserNameTag).evaluate(document).get();
                    boleBlogModel.setAuthorName(blogUserName);
                    boleBlogModel.setBlogType("转载");

                }else  if(authorNameTag.contains("本文作者")){
                    String authorUserNameTag = "//*[@id='post-"+fileName+"']/div[3]/div[1]/a[2]/text()";
                    String authorUserName = Xsoup.compile(authorUserNameTag).evaluate(document).get();

                    boleBlogModel.setAuthorName(authorUserName);
                    String boleUserNameTag = "//*[@id='post-"+fileName+"']/div[3]/div[1]/a[2]/@href";
                    String boleUserName = Xsoup.compile(boleUserNameTag).evaluate(document).get();
                    if(boleUserName != null){
                        boleBlogModel.setBoleUserName(boleUserName.replace("http://www.importnew.com/author/",""));
                    }
                    boleBlogModel.setBlogType("原创");

                    System.out.println("本文作者 = "+boleBlogModel.toString());

                }else if(authorNameTag.contains("翻译自")){
                    boleBlogModel.setBlogType("翻译");
                    String boleUserNameTag = "//*[@id='post-"+fileName+"']/div[3]/div[1]/a[2]/@href";
                    String boleUserName = Xsoup.compile(boleUserNameTag).evaluate(document).get();
                    if(boleUserName != null){
                        boleBlogModel.setBoleUserName(boleUserName.replace("http://www.importnew.com/author/",""));
                    }

                    String sourceLinkTag = "//*[@id='post-"+fileName+"']/div[3]/div[1]/a[3]/@href";
                    //博客源地址
                    String sourceLink = Xsoup.compile(sourceLinkTag).evaluate(document).get();
                    boleBlogModel.setSourceLink(sourceLink);


                }else if(authorNameTag.contains("翻译组")){
                    boleBlogModel.setBlogType("翻译");
                    String boleUserNameTag = "//*[@id='post-"+fileName+"']/div[3]/div[1]/a[2]/@href";
                    String boleUserName = Xsoup.compile(boleUserNameTag).evaluate(document).get();
                    if(boleUserName != null){
                        boleBlogModel.setBoleUserName(boleUserName.replace("http://www.importnew.com/author/",""));
                    }
                    String sourceLinkTag = "//*[@id='post-"+fileName+"']/div[3]/div[1]/a[4]/@href";
                    //博客源地址
                    String sourceLink = Xsoup.compile(sourceLinkTag).evaluate(document).get();
                    boleBlogModel.setSourceLink(sourceLink);
                }else {
                    boleBlogModel.setBlogType("其他");
                }



                String catLinkTag = "//*[@id=\"breadcrumb\"]/div";
                String catLink = Xsoup.compile(catLinkTag).evaluate(document).get();
                if(catLink != null && catLink.contains("Python")){
                    boleBlogModel.setBlogUrl("http://python.jobbole.com/"+fileName+"/");
                }else{
                    boleBlogModel.setBlogUrl("http://www.importnew.com"+"/"+fileName+".html");
                }
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
